package de.teamgamma.cansat.groundstation.export;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;

import de.teamgamma.cansat.groundstation.livestream.TransmittedValue;

/**
 * Exporter for the TXT-Export
 * @author Marc Huisinga
 *
 */
public class TxtExporter implements Exporter {

	@Override
	public void export(ExportableData input, File output) {
		File outputFile = new File(output.getAbsolutePath() + ".txt");
		boolean mkFileSucceeded = false;
		try {
			mkFileSucceeded = outputFile.createNewFile();
		} catch (IOException e1) {
			System.err.println("Invalid output file path!");
		}
		if (mkFileSucceeded) {
			TransmittedValue primaryValue = input.getValues()[0];
			TransmittedValue[] values = new TransmittedValue[input.getValues().length-1];
			int valueIndex = 0;
			for (TransmittedValue currentValue : input.getValues()) {
				if (valueIndex != 0) {
					values[valueIndex-1] = currentValue;
				}
				valueIndex ++;
			}
			HashMap<String,Integer> biggestSizes = new HashMap<String,Integer>();
			for (TransmittedValue value : input.getValues()) {
				String key = value.getKey();
				biggestSizes.put(key, key.length() + value.getUnit().length() + 4);
				for (String date : input.getDataOfKey(key)) {
					int lengthOfDate = 0;
					if (key.equals(primaryValue.getKey())) {
						lengthOfDate = new Date(Long.valueOf(date)).toString().length() + 2;
					} else {
						lengthOfDate = date.length() + 2;
					}
					if (lengthOfDate > biggestSizes.get(key)) {
						biggestSizes.put(key, lengthOfDate);
					}
				}
			}
			
			try {
				BufferedWriter currentWriter = new BufferedWriter(new FileWriter(outputFile));
				String keyInfo = primaryValue.getKey() + "(" + primaryValue.getUnit() + "): ";
				currentWriter.write(keyInfo + buildIndentation(biggestSizes.get(primaryValue.getKey()) - keyInfo.length()));
				for (TransmittedValue currentValue : values) {
					keyInfo = currentValue.getKey() + "(" + currentValue.getUnit() + ")";
					currentWriter.write(keyInfo);
					if (currentValue != values[values.length-1]) {
						currentWriter.write(", " + buildIndentation(biggestSizes.get(currentValue.getKey()) - (keyInfo.length() + 2)));
					}
				}
				currentWriter.write(System.getProperty("line.separator"));
				for (int lineIndex = 0; lineIndex < input.getDataOfKey(primaryValue.getKey()).length; lineIndex ++) {
					String value = new Date(Long.valueOf(input.getDate(primaryValue.getKey(), lineIndex))).toString() + ":  ";
					currentWriter.write(value + buildIndentation(biggestSizes.get(primaryValue.getKey()) - value.length()));
					for (TransmittedValue currentValue : values) {
						String date = input.getDate(currentValue.getKey(), lineIndex);
						currentWriter.write(date);
						if (currentValue != values[values.length-1]) {
							currentWriter.write(", " + buildIndentation(biggestSizes.get(currentValue.getKey()) - (date.length() + 2)));
						}
					}
					currentWriter.write(System.getProperty("line.separator"));
				}
				currentWriter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private String buildIndentation(int amount) {
		if (amount >= 0) {
			String indentation = "";
			for (int i = 0; i != amount; i++) {
				indentation += " ";
			}
			return indentation;
		} else {
			throw new IllegalArgumentException();
		}

	}

}
